
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>springboot(2)--数据源 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="springboot简化了我们构建应用的难度，把很多功能帮我们打包，然后我们 通过简单的注解或者引入依赖就能使用相关的功能，比如jdbc操作，缓存使用等">
<meta name="keywords" content="springboot">
<meta property="og:type" content="article">
<meta property="og:title" content="springboot(2)--数据源">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/10/10/springboot-2-数据源/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="springboot简化了我们构建应用的难度，把很多功能帮我们打包，然后我们 通过简单的注解或者引入依赖就能使用相关的功能，比如jdbc操作，缓存使用等">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb7.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb8.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb9.png">
<meta property="og:updated_time" content="2018-10-12T15:52:08.007Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="springboot(2)--数据源">
<meta name="twitter:description" content="springboot简化了我们构建应用的难度，把很多功能帮我们打包，然后我们 通过简单的注解或者引入依赖就能使用相关的功能，比如jdbc操作，缓存使用等">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/10/10/springboot-2-数据源/sb1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/10/10/springboot-2-数据源/" title="springboot(2)--数据源" itemprop="url">springboot(2)--数据源</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-10-10T15:52:53.000Z" itemprop="datePublished"> Published 2018-10-10</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#Druid连接池"><span class="toc-number">1.</span> <span class="toc-text">Druid连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Tomcat连接池"><span class="toc-number">2.</span> <span class="toc-text">Tomcat连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Dbcp连接池"><span class="toc-number">3.</span> <span class="toc-text">Dbcp连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#C3p0连接池"><span class="toc-number">4.</span> <span class="toc-text">C3p0连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#HikariCP连接池"><span class="toc-number">5.</span> <span class="toc-text">HikariCP连接池</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#总结"><span class="toc-number"></span> <span class="toc-text">总结</span></a>
		
		</div>
		
		<pre><code>    springboot简化了我们构建应用的难度，把很多功能帮我们打包，然后我们
通过简单的注解或者引入依赖就能使用相关的功能，比如jdbc操作，缓存使用等
</code></pre><a id="more"></a>
<pre><code>等。
    数据源是第三方机构基于jdbc规范实现的一套数据库操作功能，我们可以在
其基础上非常方便地实现数据查询和更新，而不必关心其底层实现，让我们把更
多的经历投入到业务实现。常见数据源(连接池)有druid，tomcat，dbcp，c3p0和
hikari等，此篇我们就基于流行的数据源实现springboot对数据库的操作。
</code></pre><h5 id="Druid连接池"><a href="#Druid连接池" class="headerlink" title="Druid连接池"></a>Druid连接池</h5><pre><code>    druid是阿里巴巴开源的数据库连接池，除了高性能之外，还提供了数据库
操作监控。
</code></pre><p><strong>1.引入pom依赖</strong></p>
<pre><code>除了引入druid依赖之外，还要添加jdbc和mysql-connector-java依赖：
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;!-- jdbcTemplate --&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div><div class="line"></div><div class="line">&lt;!-- druid数据库连接池 --&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;com.alibaba&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;druid&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div><div class="line"></div><div class="line">&lt;!-- mysql connector --&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;mysql&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</div><div class="line">    &lt;scope&gt;runtime&lt;/scope&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<p><strong>2.添加数据库连接属性配置</strong></p>
<pre><code>数据库连接属性配置文件druid.properties:
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">#数据库设置</div><div class="line">spring.datasource.driverClassName=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=true</div><div class="line">spring.datasource.username=username</div><div class="line">spring.datasource.password=password</div></pre></td></tr></table></figure>
<p><strong>3.数据库连接配置类</strong></p>
<pre><code>使用@Configuration注解类替代传统的xml配置，DruidConfiguration：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@PropertySource</span>(value = <span class="string">"classpath:druid.properties"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DruidConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span>(destroyMethod = <span class="string">"close"</span>, initMethod = <span class="string">"init"</span>,name = <span class="string">"druidDataSource"</span>)</div><div class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"spring.datasource"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">druidDataSource</span><span class="params">()</span> </span>&#123;</div><div class="line">        DruidDataSource druidDataSource = <span class="keyword">new</span> DruidDataSource();</div><div class="line">        <span class="keyword">return</span> druidDataSource;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 注册一个StatViewServlet</div><div class="line">     * <span class="doctag">@return</span></div><div class="line">     */</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">druidStatViewServlet</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="comment">//org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.</span></div><div class="line">        ServletRegistrationBean servletRegistrationBean = <span class="keyword">new</span> ServletRegistrationBean(<span class="keyword">new</span> StatViewServlet(),<span class="string">"/druid/*"</span>);</div><div class="line">        <span class="comment">//添加初始化参数：initParams</span></div><div class="line">        <span class="comment">//白名单：</span></div><div class="line">        servletRegistrationBean.addInitParameter(<span class="string">"allow"</span>,<span class="string">"127.0.0.1"</span>);</div><div class="line">        <span class="comment">//IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.</span></div><div class="line">        servletRegistrationBean.addInitParameter(<span class="string">"deny"</span>,<span class="string">"192.168.1.73"</span>);</div><div class="line">        <span class="comment">//登录查看信息的账号密码.</span></div><div class="line">        servletRegistrationBean.addInitParameter(<span class="string">"loginUsername"</span>,<span class="string">"admin"</span>);</div><div class="line">        servletRegistrationBean.addInitParameter(<span class="string">"loginPassword"</span>,<span class="string">"123456"</span>);</div><div class="line">        <span class="comment">//是否能够重置数据.</span></div><div class="line">        servletRegistrationBean.addInitParameter(<span class="string">"resetEnable"</span>,<span class="string">"false"</span>);</div><div class="line">        <span class="keyword">return</span> servletRegistrationBean;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 注册一个：filterRegistrationBean</div><div class="line">     * <span class="doctag">@return</span></div><div class="line">     */</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> FilterRegistrationBean <span class="title">druidStatFilter</span><span class="params">()</span></span>&#123;</div><div class="line">        FilterRegistrationBean filterRegistrationBean = <span class="keyword">new</span> FilterRegistrationBean(<span class="keyword">new</span> WebStatFilter());</div><div class="line">        <span class="comment">//添加过滤规则.</span></div><div class="line">        filterRegistrationBean.addUrlPatterns(<span class="string">"/*"</span>);</div><div class="line">        <span class="comment">//添加不需要忽略的格式信息.</span></div><div class="line">        filterRegistrationBean.addInitParameter(<span class="string">"exclusions"</span>,<span class="string">"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"</span>);</div><div class="line">        <span class="keyword">return</span> filterRegistrationBean;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>后边的两个@Bean是提供监控的，暂时可以忽略。
</code></pre><p><strong>4.增加配置暴露JdbcTemplate</strong></p>
<pre><code>我们此处为了测试方便，使用spring提供的jdbcTemplate：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> JdbcTemplate <span class="title">jdbcTemplate</span><span class="params">(@Qualifier(<span class="string">"druidDataSource"</span>)</span> DataSource dataSource) </span>&#123;</div><div class="line">        JdbcTemplate jdbcTemplate = <span class="keyword">new</span> JdbcTemplate(dataSource);</div><div class="line">        <span class="keyword">return</span> jdbcTemplate;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>这样我们就可以基于jdbcTemplate直接操作数据库了。
</code></pre><p><strong>5.编写测试代码&amp;测试</strong></p>
<pre><code>为了测试方便不再写dao层和service层代码，直接controller操作jdbcTemplate：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RestController</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexController</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(IndexController.class);</div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> JdbcTemplate jdbcTemplate;</div><div class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"hello"</span>)</div><div class="line">    <span class="keyword">public</span> List&lt;Map&lt;String, Object&gt;&gt; hello() &#123;</div><div class="line">        List&lt;Map&lt;String, Object&gt;&gt; list = jdbcTemplate.queryForList(<span class="string">"SELECT * FROM User "</span>, <span class="keyword">new</span> Object[]&#123;&#125;);</div><div class="line">        logger.info(<span class="string">"size:&#123;&#125;"</span>,list.size());</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行门面类Demo1Application启动应用，然后浏览器输入localhost:8080/hello看到如下结果：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb1.png" alt="springboot" title="springboot">
<pre><code>已经查到了数据，也就是我们实现了springboot使用druid数据库完成数据库操作。
</code></pre><h5 id="Tomcat连接池"><a href="#Tomcat连接池" class="headerlink" title="Tomcat连接池"></a>Tomcat连接池</h5><pre><code>Tomcat除了可以作为应用容器之外，也提供了数据库连接池。
</code></pre><p><strong>1.引入tomcat连接池依赖</strong></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;tomcat-jdbc&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<p><strong>2.添加数据库连接属性配置</strong></p>
<pre><code>数据库连接属性配置文件tomcat.properties:
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">#数据库设置</div><div class="line">spring.datasource.driverClassName=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=true</div><div class="line">spring.datasource.username=username</div><div class="line">spring.datasource.password=password</div></pre></td></tr></table></figure>
<p><strong>3.数据库连接配置类</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@PropertySource</span>(value = <span class="string">"classpath:tomcat.properties"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TomcatJdbcConfiguration</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(TomcatJdbcConfiguration.class);</div><div class="line">    <span class="meta">@Bean</span>(<span class="string">"tomcatDataSource"</span>)</div><div class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"spring.datasource"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">tomcatDataSource</span><span class="params">()</span> </span>&#123;</div><div class="line">        logger.info(<span class="string">"tomcat数据库连接池创建中......."</span>);</div><div class="line">        org.apache.tomcat.jdbc.pool.DataSource ds = <span class="keyword">new</span> org.apache.tomcat.jdbc.pool.DataSource();</div><div class="line">        <span class="keyword">return</span> ds;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p><strong>4.增加配置暴露JdbcTemplate</strong></p>
<pre><code>jdbcTemplate数据源切换成tomcatDataSource：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> JdbcTemplate <span class="title">jdbcTemplate</span><span class="params">(@Qualifier(<span class="string">"tomcatDataSource"</span>)</span> DataSource dataSource) </span>&#123;</div><div class="line">        JdbcTemplate jdbcTemplate = <span class="keyword">new</span> JdbcTemplate(dataSource);</div><div class="line">        <span class="keyword">return</span> jdbcTemplate;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>5.编写测试代码&amp;测试</strong></p>
<pre><code>重新运行门面类Demo1Application启动应用，调试jdbcTemplate注入DataSource依赖：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb2.png" alt="springboot" title="springboot">
<pre><code>这里的DataSource确实已经变成了tomcat的数据源。然后浏览器输入localhost:8080/hello看到如下结果：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb3.png" alt="springboot" title="springboot">
<pre><code>查到了数据，也就是我们实现了springboot使用tomcat连接池数据库完成数据库操作。
</code></pre><h5 id="Dbcp连接池"><a href="#Dbcp连接池" class="headerlink" title="Dbcp连接池"></a>Dbcp连接池</h5><p><strong>1.引入dbcp连接池依赖</strong></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<p><strong>2.添加数据库连接属性配置</strong></p>
<pre><code>数据库连接属性配置文件dbcp.properties:
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">#数据库设置</div><div class="line">spring.datasource.driverClassName=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=true</div><div class="line">spring.datasource.username=username</div><div class="line">spring.datasource.password=password</div></pre></td></tr></table></figure>
<p><strong>3.数据库连接配置类</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@PropertySource</span>(value = <span class="string">"classpath:dbcp.properties"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DbcpConfiguration</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(DbcpConfiguration.class);</div><div class="line">    <span class="meta">@Bean</span>(destroyMethod = <span class="string">"close"</span>,name = <span class="string">"dbcpDataSource"</span>)</div><div class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"spring.datasource"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">tomcatDataSource</span><span class="params">()</span> </span>&#123;</div><div class="line">        logger.info(<span class="string">"dbcp数据库连接池创建中......."</span>);</div><div class="line">        BasicDataSource ds = <span class="keyword">new</span> BasicDataSource();</div><div class="line">        <span class="keyword">return</span> ds;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>4.增加配置暴露JdbcTemplate</strong></p>
<pre><code>jdbcTemplate数据源切换成dbcpDataSource：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> JdbcTemplate <span class="title">jdbcTemplate</span><span class="params">(@Qualifier(<span class="string">"dbcpDataSource"</span>)</span> DataSource dataSource) </span>&#123;</div><div class="line">        JdbcTemplate jdbcTemplate = <span class="keyword">new</span> JdbcTemplate(dataSource);</div><div class="line">        <span class="keyword">return</span> jdbcTemplate;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>5.编写测试代码&amp;测试</strong></p>
<pre><code>重新运行门面类Demo1Application启动应用，调试jdbcTemplate注入DataSource依赖：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb4.png" alt="springboot" title="springboot">
<pre><code>这里的DataSource已经变成了dbcp的BasicDataSource。然后浏览器输入localhost:8080/hello看到如下结果：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb5.png" alt="springboot" title="springboot">
<pre><code>查到了数据，也就是我们实现了springboot使用dbcp连接池数据库完成数据库操作。
</code></pre><h5 id="C3p0连接池"><a href="#C3p0连接池" class="headerlink" title="C3p0连接池"></a>C3p0连接池</h5><pre><code>C3p0也是我们常用的数据库连接池，也有不错的性能和处理并发能力。
</code></pre><p><strong>1.引入dbcp连接池依赖</strong></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">    &lt;groupId&gt;com.mchange&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;c3p0&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<p><strong>2.添加数据库连接属性配置</strong></p>
<pre><code>数据库连接属性配置文件c3p0.properties:
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">#数据库设置</div><div class="line">spring.datasource.driverClass=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=true</div><div class="line">spring.datasource.user=username</div><div class="line">spring.datasource.password=password</div></pre></td></tr></table></figure>
<p><strong>3.数据库连接配置类</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@PropertySource</span>(value = <span class="string">"classpath:c3p0.properties"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">C3p0Configuration</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(C3p0Configuration.class);</div><div class="line">    <span class="meta">@Bean</span>(destroyMethod = <span class="string">"close"</span>,name = <span class="string">"c3p0DataSource"</span>)</div><div class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"spring.datasource"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">tomcatDataSource</span><span class="params">()</span> </span>&#123;</div><div class="line">        logger.info(<span class="string">"c3p0数据库连接池创建中......."</span>);</div><div class="line">        ComboPooledDataSource ds = <span class="keyword">new</span> ComboPooledDataSource();</div><div class="line">        <span class="keyword">return</span> ds;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>4.增加配置暴露JdbcTemplate</strong></p>
<pre><code>jdbcTemplate数据源切换成c3p0DataSource：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> JdbcTemplate <span class="title">jdbcTemplate</span><span class="params">(@Qualifier(<span class="string">"c3p0DataSource"</span>)</span> DataSource dataSource) </span>&#123;</div><div class="line">        JdbcTemplate jdbcTemplate = <span class="keyword">new</span> JdbcTemplate(dataSource);</div><div class="line">        <span class="keyword">return</span> jdbcTemplate;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>5.编写测试代码&amp;测试</strong></p>
<pre><code>重新运行门面类Demo1Application启动应用，调试jdbcTemplate注入DataSource依赖：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb6.png" alt="springboot" title="springboot">
<pre><code>这里的DataSource已经变成了c3p0的ComboPooledDataSource。然后浏览器输入localhost:8080/hello看到如下结果：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb7.png" alt="springboot" title="springboot">
<pre><code>查到了数据，也就是我们实现了springboot使用c3p0连接池数据库完成数据库操作。
</code></pre><h5 id="HikariCP连接池"><a href="#HikariCP连接池" class="headerlink" title="HikariCP连接池"></a>HikariCP连接池</h5><pre><code>    HikariCP连接池号称是性能最好的连接池，druid作者温少与HikariCP作者的一次对话中温少说druid默认使用了公平
锁，导致性能比HikariCP差那么一点点，但是druid扛住了双11的体量，而HikariCP没有那么大体量的真实场景验证。
</code></pre><p><strong>1.引入HikariCP连接池依赖</strong><br>        在项目引入spring-boot-starter-jdbc依赖的时候，HikariCP依赖已经被带进来了，所以此处不许额外添加依赖。</p>
<p><strong>2.添加数据库连接属性配置</strong></p>
<pre><code>数据库连接属性配置文件hikari.properties:
</code></pre><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">#数据库设置</div><div class="line">spring.datasource.driverClassName=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false</div><div class="line">spring.datasource.username=username</div><div class="line">spring.datasource.password=password</div><div class="line">spring.jooq.sql-dialect=MYSQL</div><div class="line">spring.datasource.initialize=false</div><div class="line">spring.datasource.continueOnError=true</div><div class="line">spring.datasource.hikari.idleTimeout=30000</div><div class="line">spring.datasource.hikari.maximumPoolSize=10</div></pre></td></tr></table></figure>
<p><strong>3.数据库连接配置类</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@PropertySource</span>(value = <span class="string">"classpath:hikari.properties"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HikarIConfiguration</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(HikarIConfiguration.class);</div><div class="line">    <span class="meta">@Bean</span>(<span class="string">"hikariDataSource"</span>)</div><div class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"spring.datasource"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">hikarDataSource</span><span class="params">()</span> </span>&#123;</div><div class="line">        logger.info(<span class="string">"hikari数据库连接池创建中......."</span>);</div><div class="line">        HikariDataSource ds = <span class="keyword">new</span> HikariDataSource();</div><div class="line">        <span class="keyword">return</span> ds;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>4.增加配置暴露JdbcTemplate</strong></p>
<pre><code>jdbcTemplate数据源切换成HikariDataSource：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonConfiguration</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span></div><div class="line">    <span class="function"><span class="keyword">public</span> JdbcTemplate <span class="title">jdbcTemplate</span><span class="params">(@Qualifier(<span class="string">"hikariDataSource"</span>)</span> DataSource dataSource) </span>&#123;</div><div class="line">        JdbcTemplate jdbcTemplate = <span class="keyword">new</span> JdbcTemplate(dataSource);</div><div class="line">        <span class="keyword">return</span> jdbcTemplate;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>5.编写测试代码&amp;测试</strong></p>
<pre><code>重新运行门面类Demo1Application启动应用，调试jdbcTemplate注入DataSource依赖：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb8.png" alt="springboot" title="springboot">
<pre><code>这里的DataSource已经变成了HikariCP的HikariDataSource。然后浏览器输入localhost:8080/hello看到如下结果：
</code></pre><img src="/typhoon/2018/10/10/springboot-2-数据源/sb9.png" alt="springboot" title="springboot">
<pre><code>查到了数据，也就是我们实现了springboot使用Hikari连接池数据库完成数据库操作。
</code></pre><h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><pre><code>    我们基于springboot实现了各种常见数据源的使用，性能最好的就是druid和HikariCP连接池，虽然后者号称比druid
性能好，但是基于可靠性验证，推荐使用druid，也希望通过此篇给大家在springboot日常开发中带来帮助。
</code></pre>  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/springboot/">springboot</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/springboot/">springboot</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/10/10/springboot-2-数据源/" data-title="springboot(2)--数据源 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/10/10/springboot-3-持久化/" title="springboot(3)--持久化">
  <strong>上一篇：</strong><br/>
  <span>
  springboot(3)--持久化</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/10/09/springboot-1-快速入门/"  title="springboot(1)--快速入门">
 <strong>下一篇：</strong><br/> 
 <span>springboot(1)--快速入门
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#Druid连接池"><span class="toc-number">1.</span> <span class="toc-text">Druid连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Tomcat连接池"><span class="toc-number">2.</span> <span class="toc-text">Tomcat连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Dbcp连接池"><span class="toc-number">3.</span> <span class="toc-text">Dbcp连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#C3p0连接池"><span class="toc-number">4.</span> <span class="toc-text">C3p0连接池</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#HikariCP连接池"><span class="toc-number">5.</span> <span class="toc-text">HikariCP连接池</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#总结"><span class="toc-number"></span> <span class="toc-text">总结</span></a>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
